package co.chuangdian.oflauncher.widgets;

import org.json.JSONObject;
import android.appwidget.AppWidgetHost;
import android.appwidget.AppWidgetHostView;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProviderInfo;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.FrameLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import co.chuangdian.oflauncher.R;
import co.chuangdian.oflauncher.widgets.system.AppWidget;
import co.chuangdian.oflauncher.widgets.system.ContanctsWidget;
import co.chuangdian.oflauncher.widgets.system.MusicPlayWidget;
import co.chuangdian.oflauncher.widgets.system.SettingWidget;
import co.chuangdian.oflauncher.widgets.system.WeatherWidget;

public class WidgetListRow extends FrameLayout {
	private TextView titleTextView;
	private FrameLayout placeHolder;
	private CheckBox modeCheckBox;
	ScrollView selectedScrollView;
	Context context;

	public WidgetListRow(Context context) {
		super(context);
		init(context, null);
	}

	public WidgetListRow(Context context, JSONObject widget) {
		super(context);
		init(context, widget);
	}

	AppWidgetManager appWidgetManager;

	void init(Context context, JSONObject widget) {
		this.context = context;
		
		appWidgetManager = AppWidgetManager.getInstance(getContext());
		View.inflate(getContext(), R.layout.widgets_row, this);

		titleTextView = (TextView) this.findViewById(R.id.titleTextView);
		modeCheckBox = (CheckBox) this.findViewById(R.id.modeCheckBox);
		placeHolder = (FrameLayout) this.findViewById(R.id.placeHolder);

		int id = widget.optInt("id");
		switch (id) {
		case -1:
			weatherWidgetFragment = new WeatherWidget(context);
			break;
		case -2:
			musicPlayWidgetFragment = new MusicPlayWidget(context);
			break;
		case -3:
			appWidgetFragment = new AppWidget(context);
			break;
		case -4:
			contanctsWidgetFragment = new ContanctsWidget(context);
			break;
		case -5:
			settingWidgetFragment = new SettingWidget(context);
			break;

		default:
			break;
		}

	}
	
	boolean isSystem;
	AppWidgetProviderInfo widgetInfo;
	AppWidgetHostView widgetView;
	AppWidgetHost appWidgetHost = new AppWidgetHost(getContext(), 0);
	private WeatherWidget weatherWidgetFragment;
	private MusicPlayWidget musicPlayWidgetFragment;
	private AppWidget appWidgetFragment;
	private ContanctsWidget contanctsWidgetFragment;
	private SettingWidget settingWidgetFragment;

	public void setWidget(JSONObject widget) {
		isSystem = false;
		titleTextView.setText(widget.optString("name"));
		int id = widget.optInt("id");
		this.widgetInfo = appWidgetManager.getAppWidgetInfo(id);
		widgetView = appWidgetHost.createView(getContext(), id, widgetInfo);
		placeHolder.removeAllViews();
		if (appWidgetHost == null) {
			appWidgetHost = new AppWidgetHost(getContext(), 0);
		}
		LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, widgetInfo.minHeight);
		widgetView.setLayoutParams(layoutParams);
		placeHolder.addView(widgetView);
	}

	void setControl(JSONObject widget) {
		isSystem = true;
		int id = widget.optInt("id");
		String name = widget.optString("name");
		titleTextView.setText(name);
		switch (id) {
		case -1:
			modeCheckBox.setVisibility(View.GONE);
			placeHolder.addView(weatherWidgetFragment);
			break;
		case -2:
			placeHolder.addView(musicPlayWidgetFragment);
		//	modeCheckBox.setChecked(musicPlayWidgetFragment.isShowing);
			break;
		case -3:
			placeHolder.addView(appWidgetFragment);
		//	modeCheckBox.setChecked(appWidgetFragment.bool);
			break;
		case -4:
			placeHolder.addView(contanctsWidgetFragment);
		//	modeCheckBox.setChecked(contanctsWidgetFragment.bool);
			break;
		case -5:
			placeHolder.addView(settingWidgetFragment);
		//	modeCheckBox.setChecked(settingWidgetFragment.bool);
			break;

		default:
			break;
		}
	}

	public void setOnClick(final JSONObject widget) {

		modeCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				if (widget.optBoolean("system")) {
					switch (widget.optInt("id")) {
					case -1:

						break;
					case -2:
						musicPlayWidgetFragment.setVisible(isChecked);
						break;
					case -3:
						appWidgetFragment.setVisible(isChecked);
						break;
					case -4:
						contanctsWidgetFragment.setVisible(isChecked);
						break;
					case -5:
						settingWidgetFragment.setVisible(isChecked);
						break;

					default:
						break;
					}
				} else {
					if (isChecked) {
						LayoutParams layoutParams = (LayoutParams) widgetView.getLayoutParams();
						layoutParams.width = LayoutParams.MATCH_PARENT;
						layoutParams.height = LayoutParams.MATCH_PARENT;
						widgetView.setLayoutParams(layoutParams);
					} else {
						LayoutParams layoutParams = (LayoutParams) widgetView.getLayoutParams();
						layoutParams.width = LayoutParams.MATCH_PARENT;
						layoutParams.height = widgetInfo.minHeight;
						widgetView.setLayoutParams(layoutParams);
					}
					new Handler().post(new Runnable() {
						@Override
						public void run() {
							//selectedScrollView.fullScroll(ScrollView.FOCUS_DOWN);
						}
					});

				}

			}

		});
	}
}
